package com.ruoyi.web.controller.fireman;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fireman.domain.TImage;
import com.ruoyi.fireman.service.ITImageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图片存储Controller
 *
 * @author ruoyi
 * @date 2021-08-28
 */
@Controller
@RequestMapping("/fireman/image")
public class TImageController extends BaseController {
    private String prefix = "fireman/image";

    @Autowired
    private ITImageService tImageService;

    @RequiresPermissions("fireman:image:view")
    @GetMapping()
    public String image() {
        return prefix + "/image";
    }

    /**
     * 查询图片存储列表
     */
    @RequiresPermissions("fireman:image:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TImage tImage) {
        startPage();
        LambdaQueryWrapper<TImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TImage::getDeleteFlag, 0);
        if (StringUtils.isNotEmpty(tImage.getName())) {
            queryWrapper.like(TImage::getName, tImage.getName());
        }
        if (StringUtils.isNotEmpty(tImage.getUrl())) {
            queryWrapper.like(TImage::getUrl, tImage.getUrl());
        }
        List<TImage> list = tImageService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出图片存储列表
     */
    @RequiresPermissions("fireman:image:export")
    @Log(title = "图片存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TImage tImage) {
        List<TImage> list = tImageService.selectTImageList(tImage);
        ExcelUtil<TImage> util = new ExcelUtil<TImage>(TImage.class);
        return util.exportExcel(list, "图片存储数据");
    }

    /**
     * 新增图片存储
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存图片存储
     */
    @RequiresPermissions("fireman:image:add")
    @Log(title = "图片存储", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String name, String[] urls) {
        return toAjax(tImageService.saveImageBatch(name, urls));
    }

    /**
     * 修改图片存储
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TImage tImage = tImageService.selectTImageById(id);
        mmap.put("tImage", tImage);
        return prefix + "/edit";
    }

    /**
     * 修改保存图片存储
     */
    @RequiresPermissions("fireman:image:edit")
    @Log(title = "图片存储", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TImage tImage) {
        return toAjax(tImageService.updateTImage(tImage));
    }

    /**
     * 删除图片存储
     */
    @RequiresPermissions("fireman:image:remove")
    @Log(title = "图片存储", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tImageService.deleteTImageByIds(ids));
    }
}
